package BookSystem;

//        新增图书
//        删除图书 (根据名称删除 如果当书的状态为已借，不能删除)
//        借出图书 (根据名称借书 名称不存在不能借 状态为已借也不能借)
//        归还图书 (根据名称还书 名称不存在不能还 状态为未借也不能还)
//        根据借出的天数计算租金(0.5元/天)
//        查询热度榜单
//        根据借出次数进行降序排序，将榜单显示出来

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

public class BookBiz {
    ArrayList<Book> books = new ArrayList<>();
//    PrintWriter pw;

//    public BookBiz() {
//        {
//            try {
//                pw = new PrintWriter(new FileWriter("D:/Library.txt"));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }

    /**
     * 添加书籍
     * @param name
     */
    public void addBook(String name) {
        if (books.size() != 0) {
            for (int i = 0; i < books.size(); i++) {
                if (searchName(name) != -1) {
                    System.out.println("已经有相同的书籍了！");
                    return;
                }
            }
            Book t = new Book();
            t.setName(name);
            books.add(t);
//            outputTxt(name);
        } else {
            Book t = new Book();
            t.setName(name);
            books.add(t);
        }
    }

    /**
     * 删除书籍
     * @param name
     */
    public void DeleteBook(String name) {
        switch (judgeStatus(name)) {
            case -1: {
                System.out.println("没有这本书！");
                break;
            } case 0: {
                System.out.println("抱歉，当前书籍已经借出，请等归还再删除！");
                break;
            } case 1: {
                books.remove(searchName(name));
                books.trimToSize();
                System.out.println("删除成功！");
                break;
            }
        }
    }

    /**
     * 借书
     * @param name
     */
    public void lendBook(String name) {
        switch (judgeStatus(name)) {
            case -1: {
                System.out.println("没有这本书！");
                break;
            } case 0: {
                System.out.println("抱歉，当前书籍已经借出，请等归还再借！");
                break;
            } case 1: {
                Book t = books.get(searchName(name));
                t.setStatus("已借");
                t.setLoanDate(new Date());
                t.setCount((t.getCount()) + 1);
                this.books.set(judgeStatus(name), t);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
                System.out.println("借出成功！");
                break;
            }
        }
    }

    /**
     * 还书
     * @param name
     */
    public void returnBook(String name) {
        switch (judgeStatus(name)) {
            case -1: {
                System.out.println("没有这本书！");
                break;
            } case 0: {
                Book t = books.get(searchName(name));
                double money = calculationMoney(name);
                t.setStatus("未接");
//                t.setLoanDate(null);
                zeroLoanDate(name);
                this.books.set(judgeStatus(name), t);
                System.out.println("归还成功！");
                System.out.println("佣金为 ￥" + money);
                break;
            } case 1: {
                System.out.println("抱歉，当前书籍已经未借出，无法归还！");
                break;
            }
        }
    }

    /**
     * 输出前3
     */
    public void popularityList() {
        sortCount();
        int t = 3;
        menuTool("热门榜单");
        try {
            for (int i = 0; i < t; i++) {
                System.out.println(books.get(i));
            }
        } catch (IndexOutOfBoundsException e) {
//            e.printStackTrace();
        }
        menuTool();
    }
    /**
     * 输出指定数目
     */
    public void popularityList(int count) {
        sortCount();

        menuTool("热门榜单");
        if (count <= books.size()) {
            for (int i = 0; i < count; i++) {
                System.out.println(books.get(i));
            }
        } else {
            for (int i = 0; i < books.size(); i++) {
                System.out.println(books.get(i));
            }
        }
        menuTool();
    }

    /**
     * 总清单
     */
    public void menu() {
        if (books.size() != 0) {
            menuTool("目录");
            for (Book book : books) {
                System.out.println(book);
            }
            menuTool();
        } else {
            System.out.println("抱歉，当前图书系统中没有藏书！");
        }
    }


    /**
     * 计算金额
     * @param name
     * @return money
     */
    public double calculationMoney(String name) {
        long diffDays = diffDays(name);
        double rent = 0.5;

        return diffDays * rent;
    }

    /**
     * 计算借出去的天数
     * @param name
     * @return diffDays
     */
    public long diffDays(String name) {
        Book t = books.get(searchName(name));
        Date time_1 = t.getLoanDate();
        Date time_2 = new Date();

        //单位是毫秒（ms）
        long diff = time_2.getTime() - time_1.getTime();

        return diff / (24 * 60 * 60 * 1000);
    }

    /**
     * 排序
     * 规则为排序书本借出次数
     */
    public void sortCount() {
        //JDK1.8独有语法
        books.sort(new Comparator<Book>() {
            @Override
            public int compare(Book c1, Book c2) {
                if (c1.getCount() > c2.getCount())
                    return -1;
                else if (c1.getCount() < c2.getCount())
                    return 1;
                else return 0;
            }
        });
        //JDK1.7的语法
        Collections.sort(books,new Comparator<Book>() {
            @Override
            public int compare(Book c1,Book c2) {
                if(c1.getCount() >c2.getCount())
                    return -1;
                else if(c1.getCount()<c2.getCount())
                    return 1;
                else return 0;
            }
        });
    }


    /**
     * 判断书的借出状态，无此书就返回-1，已借就返回0，未借就返回1。
     * @param name
     * @return  i
     */
    public int judgeStatus(String name) {
        int index = searchName(name);

        if (books.size() == 0) {
            return -1;
        }
        if (index != -1) {
            if ("未借".equals(this.books.get(index).getStatus())) {
                return 1;
            } else if ("已借".equals(this.books.get(index).getStatus())) {
                return 0;
            }
        }
        return -1;
    }

    /**
     * 判断是否有这本书, 没有就返回-1，有就返回下标
     * @param name
     * @return index
     */
    public int searchName(String name) {
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getName().equals(name)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 接受书的名字，将日期归零
     * @param name
     */
    public void zeroLoanDate(String name) {
        int index = searchName(name);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

        if (index != -1) {
            Book t = books.get(index);
            try {
                t.setLoanDate(sdf.parse("0000年00月00日 00时00分00秒"));
            } catch (ParseException e) {
//            e.printStackTrace();
            }
            books.set(index, t);
        }
    }

    /**
     * 方便制作清单，扩充BookBiz类时用上
     * @param title
     */
    public void menuTool(String title) {
        System.out.println("****************************************************************\n");
        System.out.println("\t\t\t\t\t\t[" + title + "]\n");
        System.out.println("\t[" + "书名" + "]\t[" + "状态" + "]\t[" + "借出时间" + "]\t\t\t\t\t[" + "借出次数" + "]\n");
    }
    /**
     * 方便制作清单，扩充BookBiz类时用上
     * @param
     */
    public void menuTool() {
        System.out.println("****************************************************************\n");
    }

    /**
     * 方便制作TXT清单
     * @param name
     */
//    public void outputTxt(String name) {
//        int index = searchName(name);
////        PrintWriter pw = null;
//
////            pw = new PrintWriter(new FileWriter("D:/library.txt"));
//        pw.println(books.get(index));
////            pw.close();
//        pw.flush();
//    }
}
